package DAO;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import obj.Support;
import DAO.DAO;

public class SupportDAO extends DAO<Support> {

	public boolean create(Support obj) {
		try {
			PreparedStatement ps = this.connect
					.prepareStatement("INSERT INTO SUPPORT(S_LIB,S_TX_USURE) VALUES('"+obj.getLib()+"',"+obj.getTxU()+")");
			ps.executeUpdate();
			ps.close();
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public Support find(String id) {
		Support sup = new Support();
		try {
			ResultSet rs = this.connect.createStatement(
					ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_UPDATABLE).executeQuery(
					"SELECT * FROM SUPPORT WHERE S_LIB='" + id+"'");
			if (rs.first()) {
				sup = new Support(id,rs.getDouble("S_TX_USURE"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return sup;
	}

	public boolean update(Support obj) {
		try {
			PreparedStatement ps = this.connect
					.prepareStatement("UPDATE SUPPORT SET S_TX_USURE='"+obj.getTxU()+"' WHERE S_LIB ='"+obj.getLib()+"'");
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public void delete(Support obj) {
		try {
			PreparedStatement ps = this.connect
					.prepareStatement("DELETE FROM SUPPORT WHERE S_LIB ='"+obj.getLib()+"'");
			ps.executeUpdate();
			ps.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public ArrayList<String> selectAll() {
		ArrayList<String> ltype= new ArrayList<String>();
		try {
			ResultSet rs = this.connect.createStatement(
					ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_UPDATABLE).executeQuery(
					"SELECT * FROM SUPPORT");
			
			while(rs.next()){
				ltype.add(rs.getString(1));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return ltype;
	}
}

